package com.demo.core.dao.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import javax.sql.DataSource;

import com.demo.core.dao.FileDao;
import com.demo.core.dao.impl.base.PageCoreDaoSupport;
import com.demo.core.model.File;
import com.demo.core.model.support.GeneralExecuteModel;

/**
 * 配置数据访问层实现类
 * 
 * @author ryuu.kk
 */
public class FileDaoImpl extends PageCoreDaoSupport<File, Long> implements FileDao {
	
	/**
	 * 配置空间 
	 */
	protected final static String FILE_SPACE = "fileSpace.";
	
	/**
	 * 模型表名
	 */
	protected final static String TABLE_NAME = "T_CORE_FILE";
	
	/**
	 * 模型表主键ID
	 */
	protected final static String TABLE_PK_NAME = "ID";
	
	/**
	 * 直接取得
	 */
	protected final static String FILE_CONTENT_SQL = " SELECT %s FROM T_CORE_FILE WHERE ID = %d";
	
	/**
	 * 表名称
	 */
	protected final static String FILE_COLUMN_CONTENT ="CONTENT";
	
	/**
	 * 缓冲字节
	 */
	private static final int BUFFER_SIZE = 1024;
	
	@Override
	public int updateStatus(File file, Map<String, Object> fileStatus) {
		cacheClear();
		fileStatus.put("file", file);
		return getSqlMapClientTemplate().update(FILE_SPACE + "changeEnabled", fileStatus);
	}
	@Override
	public byte[] getContent(Long fileId) {
		File file = (File) getSqlMapClientTemplate().queryForObject(FILE_SPACE + "queryBitFile", fileId);
		return (byte[]) file.getContent();
	}

	@Override
	public InputStream getFileInputStream(Long fileId) {
		Blob blob = null;
		Connection conn = null;
		ResultSet rs = null;
		Statement statement = null;
		DataSource ds = getSqlMapClient().getDataSource();
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {
			conn = ds.getConnection();
			if (conn != null && !conn.isClosed()) {
				statement = conn.createStatement();
				rs = statement.executeQuery(String.format(FILE_CONTENT_SQL, FILE_COLUMN_CONTENT, fileId));
				if (rs.first()) {
					blob = rs.getBlob(FILE_COLUMN_CONTENT);
					//return blob.getBinaryStream();
					InputStream in = blob.getBinaryStream();
					int count;
					byte[] buffer = new byte[BUFFER_SIZE];
			        while ((count = in.read(buffer)) > 0) {
			        	outputStream.write(buffer, 0, count);
			        }
					return new ByteArrayInputStream(outputStream.toByteArray());
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
				if (rs != null) {
					try {
						rs.close();
					} catch (Exception e) {
						// dosomething
					}
				}
				if (statement != null) {
					try {
						statement.close();
					} catch (Exception e) {
						// dosomething
					}
				}
				if (conn != null) {
					try {
						conn.close();
					} catch (SQLException e) {
						// dosomething
					}
				}
		}
		return null;
	}
	@Override
	protected String nameSpace() {
		return model.getNameSpace();
	}
	@Override
	protected void doInit(GeneralExecuteModel<Long> model) {
		model.setPk(TABLE_PK_NAME);
		model.setTable(TABLE_NAME);
		model.setNameSpace(FILE_SPACE);
	}
}